# -*- coding:utf-8 -*-
#! python3

from facecat import *
import timer
import random

#更新悬浮状态
#views:视图集合
def updateView(views):
	updateViewDefault(views)
	for i in range(0,len(views)):
		view = views[i]
		if view.viewName == "price":
			view.columns[0].width = view.size.cx


#绘制视图
#view:视图
#paint:绘图对象
#clipRect:区域
def onPaint(view, paint, clipRect):
	onPaintDefault(view, paint, clipRect)
	if view.viewName == "list":
		for i in range(0, len(view.views)):
			if view.views[i].isMove == False:
				drawListItem(view.views[i], paint, clipRect)

#绘制视图边线
#view:视图
#paint:绘图对象
#clipRect:区域
def onPaintBorder(view, paint, clipRect):
	onPaintBorderDefault(view, paint, clipRect)
	if view.viewName == "list":
		for i in range(0, len(view.views)):
			if view.views[i].isMove:
				drawListItem(view.views[i], paint, clipRect)

#视图的鼠标移动方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseMove(view, mp, buttons, clicks, delta):
	onMouseMoveDefault(view, mp, buttons, clicks, delta)
		
#视图的鼠标按下方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseDown(view, mp, buttons, clicks, delta):
	onMouseDownDefault(view, mp, buttons, clicks, delta)

#视图的鼠标抬起方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseUp(view, mp, buttons, clicks, delta):
	onMouseUpDefault(view, mp, buttons, clicks, delta)

#视图的鼠标点击方法
#view 视图
#firstTouch:是否第一次触摸 
#firstPoint:第一次触摸的坐标 
#secondTouch:是否第二次触摸 
#secondPoint:第二次触摸的坐标
#clicks 点击次数
def onClick(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks):
	onClickDefault(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks)

#视图的鼠标滚动方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseWheel(view, mp, buttons, clicks, delta):
	onMouseWheelDefault(view, mp, buttons, clicks, delta)
	if view.viewType == "ldata":
		touchWheelDiv(view.parent, delta)
		invalidateView(view.parent)

appPaint = FCPaint() #创建绘图对象
appPaint.defaultUIStyle = "dark"
#注册事件
appPaint.onPaint = onPaint 
appPaint.onPaintBorder = onPaintBorder
appPaint.onMouseDown = onMouseDown 
appPaint.onMouseMove = onMouseMove 
appPaint.onMouseUp = onMouseUp
appPaint.onMouseWheel = onMouseWheel
appPaint.onClick = onClick
appPaint.onUpdateView = updateView

#消息循环
def WndProc(hwnd,msg,wParam,lParam):
	return WndProcDefault(appPaint,hwnd,msg,wParam,lParam)

#面积图数据
class ListData(FCView):
	def __init__(self):
		super().__init__()
		self.value = 0 #数值
		self.key = None #键
		self.close = 0 
		self.open = 0
		self.viewType = "ldata" #类型
		self.isMove = False #是否正在移动

#绘制数据项
def drawListItem(view, paint, clipRect):
	scrollV = view.parent.scrollV
	if view.location.y + view.size.cy - scrollV >= 0 and view.location.y - scrollV < view.parent.size.cy:
		totalValue = view.parent.totalValue
		diffRange = toFixed((view.close - view.open) / view.close * 100, 2) + "%"
		rtRight = (view.size.cx - 130) * view.value / totalValue
		if view.backColor != "none":
			paint.fillRect(view.backColor, 0, 10 - scrollV + view.location.y, rtRight, view.size.cy - 10 - scrollV + view.location.y)
		if view.borderColor != "none":
			paint.drawRect(view.borderColor, 1, 0, 0, 10 - scrollV + view.location.y, rtRight, view.size.cy - 10 - scrollV + view.location.y)
		rtRight += 10
		fontSize1 = int(min(view.size.cx, view.size.cy) / 4);
		if fontSize1 > 1:
			baseUpper = "Code1"
			font1 = "Default," + str(fontSize1)
			tSize = paint.textSize(baseUpper, font1)	           
			quoteUpper = "Code2"
			font2 = "Default," + str(fontSize1 / 2)
			tSize2 = paint.textSize(quoteUpper, font2)
	            
			if view.paint.defaultUIStyle == "dark":
				paint.drawText(baseUpper, "rgb(255,255,255)", font1, rtRight, view.size.cy / 2 - tSize.cy + 2 - scrollV + view.location.y - tSize.cy / 2)
				paint.drawText(quoteUpper, "rgb(255,255,255)", font2, rtRight, view.size.cy / 2 + 2 - scrollV + view.location.y - tSize.cy / 2)
			elif view.paint.defaultUIStyle == "light":
				paint.drawText(baseUpper, "rgb(0,0,0)", font1, rtRight, view.size.cy / 2 - tSize.cy + 2 - scrollV + view.location.y - tSize.cy / 2)
				paint.drawText(quoteUpper, "rgb(0,0,0)", font2, rtRight, view.size.cy / 2 + 2 - scrollV + view.location.y - tSize.cy / 2)
	            
			strPrice = toFixed(view.close, 6)
			font3 = "Default," + str(fontSize1 * 2 / 3)
			tSize5 = paint.textSize(strPrice, font3)
			if view.paint.defaultUIStyle == "dark":
				paint.drawText(strPrice, "rgb(255,255,255)", font3, rtRight, view.size.cy / 2 + tSize.cy + 2 - scrollV + view.location.y - tSize.cy / 2)
			elif view.paint.defaultUIStyle == "light":
				paint.drawText(strPrice, "rgb(0,0,0)", font3, rtRight, view.size.cy / 2 + tSize.cy + 2 - scrollV + view.location.y - tSize.cy / 2)
	
#更新列表
#dynaList 动态列表
def updateList(dynaList):
	dynaList.rects = []
	viewsSize = len(dynaList.views)
	for i in range(0, viewsSize):
		thisCell = dynaList.views[i]
		dynaList.rects.append(FCRect(0, i * dynaList.itemHeight, dynaList.size.cx - dynaList.scrollSize, (i + 1) * dynaList.itemHeight));

#秒表事件
#dynaList 动态列表
def onListTime(dynaList):
	paint2 = False
	if dynaList.useAnimation:
		for i in range(0, len(dynaList.rects)):
			subView = dynaList.views[i]
			targetRect = dynaList.rects[i]
			nowRect = FCRect(subView.location.x, subView.location.y, subView.location.x + subView.size.cx, subView.location.y + subView.size.cy)
			isMove = False
			if 1 == 1:
				if nowRect.left > targetRect.left:
					nowRect.left -= (nowRect.left - targetRect.left) / 4
					if nowRect.left - targetRect.left < 10:
						nowRect.left = targetRect.left
					paint2 = True
				elif nowRect.left < targetRect.left:
					nowRect.left += (targetRect.left - nowRect.left) / 4
					if targetRect.left - nowRect.left < 10:
						nowRect.left = targetRect.left
					paint2 = True
			if 1 == 1:
				if nowRect.top > targetRect.top:
					nowRect.top -= (nowRect.top - targetRect.top) / 4
					if nowRect.top - targetRect.top < 10:
						nowRect.top = targetRect.top
					paint2 = True
					isMove = True
				elif nowRect.top < targetRect.top:
					nowRect.top += (targetRect.top - nowRect.top) / 4
					if targetRect.top - nowRect.top < 10:
						nowRect.top = targetRect.top
					paint2 = True
			if 1 == 1:
				if nowRect.right > targetRect.right:
					nowRect.right -= (nowRect.right - targetRect.right) / 4
					if nowRect.right - targetRect.right < 10:
						nowRect.right = targetRect.right
					paint2 = True
				elif nowRect.right < targetRect.right:
					nowRect.right += (targetRect.right - nowRect.right) / 4
					if targetRect.right - nowRect.right < 10:
						nowRect.right = targetRect.right
					paint2 = True
			if 1 == 1:
				if nowRect.bottom > targetRect.bottom:
					nowRect.bottom -= (nowRect.bottom - targetRect.bottom) / 4
					if nowRect.bottom - targetRect.bottom < 10:
						nowRect.bottom = targetRect.bottom
					paint2 = True
				elif nowRect.bottom < targetRect.bottom:
					nowRect.bottom += (targetRect.bottom - nowRect.bottom) / 4
					if targetRect.bottom - nowRect.bottom < 10:
						nowRect.bottom = targetRect.bottom
					paint2 = True
			subView.isMove = isMove;
			subView.location = FCPoint(nowRect.left, nowRect.top)
			subView.size = FCSize(nowRect.right - nowRect.left, nowRect.bottom - nowRect.top)
	else:
		for i in range(0, len(dynaList.rects)):
			subView = dynaList.views[i]
			targetRect = dynaList.rects[i]
			subView.location = FCPoint(targetRect.left, targetRect.top)
			subView.size = FCSize(targetRect.right - targetRect.left, targetRect.bottom - targetRect.top)
	if paint2:
		invalidateView(dynaList)

#创建窗体
createMainWindow(appPaint, "facecat-py", WndProc)

#默认颜色
listColors = []
listColors.append("rgb(59,174,218)")
listColors.append("rgb(185,63,150)")
listColors.append("rgb(219,68,83)")
listColors.append("rgb(246,187,67)")
listColors.append("rgb(216,112,173)")
listColors.append("rgb(140,192,81)")
listColors.append("rgb(233,87,62)")
listColors.append("rgb(150,123,220)")
listColors.append("rgb(75,137,220)")
listColors.append("rgb(170,178,189)")

#创建列表
listView = FCView()
listView.viewType = "div"
listView.backColor = "rgb(0,0,0)"
listView.scrollBarColor = "rgb(100,100,100)"
listView.viewName = "list"
#填充布局
listView.dock = "fill"
listView.itemHeight = 60
listView.rects = []
listView.useAnimation = True
listView.showVScrollBar = True
#添加到框架中
addView(listView, appPaint)

#检查数据并更新界面
def checkNewData(a='', b=''):
	global appPaint
	global listView
	updateList(listView)
	onListTime(listView)
	invalidate(appPaint)

for i in range(0,100):
	pData = ListData()
	pData.key = str(i)
	pData.text = str(i)
	pData.open = random.randint(10, 100)
	pData.close = random.randint(10, 100)
	pData.value = random.randint(10, 10000)
	pData.size = FCSize(listView.size.cx, listView.itemHeight)
	pData.location = FCPoint(0, len(listView.views) * listView.itemHeight)
	pData.backColor = listColors[len(listView.views) % len(listColors)]
	pData.allowDraw = False
	if listView.paint.defaultUIStyle == "dark":
		pData.borderColor = "rgb(255,255,255)"
	elif listView.paint.defaultUIStyle == "light":
		pData.borderColor = "rgb(0,0,0)"
	#添加到父视图
	addViewToParent(pData, listView)
maxValue = 0; 
viewsSize = len(listView.views)
#计算最大值
for i in range(0, viewsSize):
	thisCell = listView.views[i]
	if maxValue < thisCell.value:
	    maxValue = thisCell.value
listView.totalValue = maxValue
#排序
listView.views = sorted(listView.views, key=attrgetter('value'), reverse=True)

#设置秒表
timer.set_timer(50, checkNewData)
#获取窗体大小
rect = win32gui.GetClientRect(appPaint.hWnd)
#更新布局
appPaint.size = FCSize(rect[2] - rect[0], rect[3] - rect[1])
updateView(appPaint.views)
showWindow(appPaint)